package com.googlecode.biglog.filter;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

public class StringStartsWithFilter extends Filter {
	private String startsWith;
	private String action;
	public String getStartsWith() {
		return startsWith;
	}
	public void setStartsWith(String startsWith) {
		this.startsWith = startsWith;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}

	@Override
	public int decide(LoggingEvent event) {
		if (startsWith == null)
			return Filter.NEUTRAL;

		String msg = event.getRenderedMessage();

		if (msg == null || msg.indexOf(startsWith) == -1)
			return action == null || "pass".equals(action) ? Filter.DENY : Filter.NEUTRAL;

		msg = msg.trim();
		while (msg.length() > 1 && (msg.startsWith("\t") || msg.startsWith("\r") || msg.startsWith("\n"))) {
			msg = msg.substring(1);
			msg = msg.trim();
		}

		if (msg.startsWith(startsWith))
			return action == null || "pass".equals(action) ? Filter.NEUTRAL : Filter.DENY;

		return action == null || "pass".equals(action) ? Filter.DENY : Filter.NEUTRAL;
	}

}
